<?php
/**
 * 上传附件和上传视频
 * User: Jinqn
 * Date: 14-04-09
 * Time: 上午10:17
 */
include "Uploader.class.php";

/* 上传配置 */
$base64 = "upload";
switch (htmlspecialchars($_GET['action'])) {
    case 'uploadimage':
        $config = array(
            "pathFormat" => $CONFIG['imagePathFormat'],
            "maxSize" => $CONFIG['imageMaxSize'],
            "allowFiles" => $CONFIG['imageAllowFiles']
        );
        $fieldName = $CONFIG['imageFieldName'];
        break;
    case 'uploadscrawl':
        $config = array(
            "pathFormat" => $CONFIG['scrawlPathFormat'],
            "maxSize" => $CONFIG['scrawlMaxSize'],
            "allowFiles" => $CONFIG['scrawlAllowFiles'],
            "oriName" => "scrawl.png"
        );
        $fieldName = $CONFIG['scrawlFieldName'];
        $base64 = "base64";
        break;
    case 'uploadvideo':
        $config = array(
            "pathFormat" => $CONFIG['videoPathFormat'],
            "maxSize" => $CONFIG['videoMaxSize'],
            "allowFiles" => $CONFIG['videoAllowFiles']
        );
        $fieldName = $CONFIG['videoFieldName'];
        break;
    case 'uploadfile':
    default:
        $config = array(
            "pathFormat" => $CONFIG['filePathFormat'],
            "maxSize" => $CONFIG['fileMaxSize'],
            "allowFiles" => $CONFIG['fileAllowFiles']
        );
        $fieldName = $CONFIG['fileFieldName'];
        break;
}

/* 生成上传实例对象并完成上传 */
$up = new Uploader($fieldName, $config, $base64);

/**
 * 得到上传文件所对应的各个参数,数组结构
 * array(
 *     "state" => "",          //上传状态，上传成功时必须返回"SUCCESS"
 *     "url" => "",            //返回的地址
 *     "title" => "",          //新文件名
 *     "original" => "",       //原始文件名
 *     "type" => ""            //文件类型
 *     "size" => "",           //文件大小
 * )
 */

/* 返回数据 */
$fileInfo = $up->getFileInfo();
if(in_array($_GET['action'], ['uploadimage', 'uploadfile'])) {
    $type = ($_GET['action'] == 'uploadimage') ? 'pic' : 'file';
    //TODO 上传到远程文件服务器 unlink本地文件 curl 写接口
    $upRes = picStore($fileInfo['file_path'], $type);
    if(!$upRes) {
        //TODO return
    }
    unlink($fileInfo['file_path']);
    if($type == 'pic') {
        $fileInfo['url'] = 'http://clubimg.lenovo.com.cn' . '/pic/'. $upRes['picid'] .'/0';
    }else{
        $fileInfo['url'] = 'http://img2cdn.clubstatic.lenovo.com.cn/download/' . $upRes['filename'];
    }

}

return json_encode($fileInfo);




/**
 *	文件储存
 */
function picStore($file, $type)
{
    $picServer = 'http://clubimg.lenovo.com.cn';
    $ip = $_SERVER['SERVER_ADDR'];
    if($type == 'pic') {
        $uploadType = 'upload_pic';
        $fields = ['pic' => new \CURLFile($file)];
    }elseif ($type == 'file') {
        $uploadType = 'upload_file';
        $fields = ['file' => new \CURLFile($file)];
    }
    $url = $picServer.'/file/' . $uploadType . '?ip=' . $ip;//target url

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    if ($error = curl_error($ch)) {
        die($error);
    }
    curl_close($ch);
    $result = json_decode($result, true);
    if($type == 'pic') {
        if ($result && isset($result["picid"])) {
            return $result;
        }

    }elseif ($type == 'file') {

        if ($result && isset($result["filename"])) {
            return $result;
        }
    }
    return array();

}
